package de.wittekind.bundesjugendspiele.client.management.ui.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;

import de.wittekind.bundesjugendspiele.client.management.conf.Constants;
import de.wittekind.bundesjugendspiele.client.management.ui.UIController;
import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler.Gender;

public class SchuelerDetailView extends DetailView {

	Schueler s;
	private static final long serialVersionUID = 6496504645433565564L;
	private JTextField firstName, lastName;
	JFormattedTextField date;
	private ButtonGroup genderGroup;

	public SchuelerDetailView(Schueler s, UIController ui) throws IOException {
		super(ui);

		
		displaySchueler(s);
	}
	public void displaySchueler(Schueler s) throws IOException {

		
		this.s = s;
		
		GridBagConstraints c;

		JLabel l = new JLabel(Constants.msg.getString("firstName"));

		c = getStandardConstraints();
		this.add(l, c);

		firstName = new JTextField(s.getFirstName());
		firstName.setColumns(10);
		
		c = getStandardConstraints();
		this.add(firstName, c);

		l = new JLabel(Constants.msg.getString("lastName"));

		c = getStandardConstraints();
		this.add(l, c);

		lastName = new JTextField(s.getLastName());
		lastName.setColumns(10);
		
		c = getStandardConstraints();
		this.add(lastName, c);
		
		l = new JLabel(Constants.msg.getString("gender"));
		
		c = getStandardConstraints();
		this.add(l, c);

		Gender g = s.getGender();
		genderGroup = new ButtonGroup();
		JRadioButton male = new JRadioButton(Constants.msg.getString("male"));
		male.setMnemonic(1);
		if (g == Gender.MALE)
			male.setSelected(true);
		genderGroup.add(male);

		JRadioButton female = new JRadioButton(Constants.msg.getString("female"));
		female.setMnemonic(2);
		if (g == Gender.FEMALE)
			female.setSelected(true);
		genderGroup.add(female);

		JRadioButton other = new JRadioButton(Constants.msg.getString("notKnown"));
		male.setMnemonic(9);
		if (g == Gender.NOT_SPECIFIED)
			other.setSelected(true);
		genderGroup.add(other);

		GridLayout genderLayout = new GridLayout(3, 1);
		JPanel gender = new JPanel();
		gender.setLayout(genderLayout);
		gender.add(male);
		gender.add(female);
		gender.add(other);
		
		

		c = getStandardConstraints();
		this.add(gender, c);

		l = new JLabel(Constants.msg.getString("birthDate"));

		c = getStandardConstraints();
		this.add(l, c);

		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		DateFormatter formatter = new DateFormatter(format);
		format.setLenient(false);
		formatter.setAllowsInvalid(false);
		formatter.setOverwriteMode(true);
		date = new JFormattedTextField(formatter);
		date.setText(format.format(s.getBirthday().getTime()));
		date.setColumns(10);
		
		c = getStandardConstraints();
		this.add(date, c);

		save = new JButton(Constants.msg.getString("save"));
		save.setName("schuelerSave");
		save.setSize(new Dimension(20, 20));
		
		c = getStandardConstraints();
		this.add(save, c);
	}

	public Date getBDay() {
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		Date d = null;
		try {
			d = (Date) format.parseObject(date.getText());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return d;
	}

	public String getFirstName() {

		return firstName.getText();
	}

	public String getLastName() {

		return lastName.getText();
	}

	public Gender getGender() {
		int i = 0;
		if (genderGroup.getSelection() != null)
			i = genderGroup.getSelection().getMnemonic();
		Gender g;
		switch (i) {
		case 1:
			g = Gender.MALE;
			break;
		case 2:
			g = Gender.FEMALE;
			break;
		case 9:
			g = Gender.NOT_SPECIFIED;
			break;
		default:
			g = Gender.NOT_KNOWN;
			break;
		}

		return g;
	}

}
